﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;

namespace OALPTool
{
    /// <summary>
    /// 
    /// </summary>
    public class DataGridViewFunc
    {
        /// <summary>
        /// 
        /// </summary>
        public DataGridViewFunc() 
        {

        }

        /// <summary>
        /// 设置DataGridView样式
        ///   0:字段中文名；
        ///   1:字段英文名；
        ///   2:值类型【0-TextBox(默认),1-CheckBox,2-ComboBox,3-DateTimePicker,6-TextBox(自动换行),7-超链接】；
        ///   3:显示宽度；
        ///   4:显示格式化【"c"表示数字格式化；"d"表示时间格式化】；
        ///   5:对齐方式；
        ///   6:是否只读【true为只读；false为可修改】；
        ///   7:数据为空时显示；
        ///   8:是否存在转义；
        ///   9:显示顺序号；
        ///   10:颜色字段名【暂时未用】；
        ///   11:颜色字段意义列表【暂时未用】；
        ///   12:格式化字段；
        ///   13:暂时没用；
        ///   14:ArrayList --Combol项集合对应数据集【每条数据对应一个object集合，object[0]为Display值，object[1]为Value值】
        /// <param name="arr_style">DataGridView样式/param>
        /// <param name="dgv">DataGridView</param>
        /// <param name="dv">绑定数据集</param>
        /// </summary>
        public static void setDgvStyle(ArrayList arr_style, ref DataGridView dgv, DataView dv) 
        {
            dgv.DataSource = dv;

            dgv.Columns.Clear();

            int i = 0;
            foreach(object col in arr_style)
            {
                Object[] obj = (Object[])col;

                if (obj != null && obj.Length <= 15)
                {
                    if (obj[2].ToString() == "0" || obj[2].ToString() == "6")
                    {
                        dgv.Columns.Add(CreateTextBoxColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "1")
                    {
                        dgv.Columns.Add(CreateCheckBoxColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "2")
                    {
                        dgv.Columns.Add(CreateComboBoxColumn(obj[1].ToString(), obj[0].ToString(), (ArrayList)obj[14]));
                    }
                    else if (obj[2].ToString() == "3")
                    {
                        dgv.Columns.Add(CreateDateTimeColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "7")
                    {
                        dgv.Columns.Add(CreateLinkColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else
                    {
                        continue;
                    }

                    if (obj[3] != null && obj[3].ToString() != String.Empty)
                    {
                        dgv.Columns[i].Width = (int)obj[3];
                    }
                    if (obj[4] != null)
                    {
                        if (obj[4].ToString() == "c")
                        {
                            dgv.Columns[i].ValueType = typeof(Double);
                            if (obj.Length > 12)
                            {
                                if (obj[12] == null || obj[12].ToString() == String.Empty)
                                {
                                    dgv.Columns[i].DefaultCellStyle.Format = "c";
                                }
                                else
                                {
                                    dgv.Columns[i].DefaultCellStyle.Format = obj[12].ToString();
                                }
                            }
                            else dgv.Columns[i].DefaultCellStyle.Format = "c";
                        }
                        else if (obj[4].ToString() == "d")
                        {
                            dgv.Columns[i].ValueType = typeof(DateTime);
                            if (obj.Length > 12)
                            {
                                if (obj[12] == null || obj[12].ToString() == String.Empty)
                                {
                                    dgv.Columns[i].DefaultCellStyle.Format = "d";
                                }
                                else
                                {
                                    dgv.Columns[i].DefaultCellStyle.Format = obj[12].ToString();
                                }
                            }
                            else dgv.Columns[i].DefaultCellStyle.Format = "d";

                        }
                    }
                    dgv.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    if (obj.Length > 5 && obj[5] != null)
                    {
                        dgv.Columns[i].DefaultCellStyle.Alignment = (DataGridViewContentAlignment)obj[5];
                    }
                    else 
                    {
                        dgv.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }
                    if (obj.Length > 6 && obj[6] != null)
                    {
                        dgv.Columns[i].ReadOnly = (bool)obj[6];
                    }
                    else
                    {
                        dgv.Columns[i].ReadOnly = true;
                    }
                    if (obj.Length > 7 && obj[7] != null)
                    {
                        dgv.Columns[i].DefaultCellStyle.NullValue = obj[7].ToString();
                    }


                    if (obj[2].ToString() == "6")
                    {
                        dgv.Columns[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    }

                    dgv.Columns[i].DisplayIndex = i;
                }

                i++;
            }

            dgv.AutoGenerateColumns = false;
        }


        /// <summary>
        /// 设置DataGridViewHead样式
        ///   0:字段中文名；
        ///   1:字段英文名；
        ///   2:值类型【0-TextBox(默认),1-CheckBox,2-ComboBox,3-DateTimePicker,6-TextBox(自动换行),7-超链接】；
        ///   3:显示宽度；
        ///   4:显示格式化【"c"表示数字格式化；"d"表示时间格式化】；
        ///   5:对齐方式；
        ///   6:是否只读【true为只读；false为可修改】；
        ///   7:数据为空时显示；
        ///   8:是否存在转义；
        ///   9:显示顺序号；
        ///   10:颜色字段名【暂时未用】；
        ///   11:颜色字段意义列表【暂时未用】；
        ///   12:格式化字段；
        ///   13:暂时没用；
        ///   14:ArrayList --Combol项集合对应数据集【每条数据对应一个object集合，object[0]为Display值，object[1]为Value值】
        /// </summary>
        /// <param name="arr_style">DataGridView样式</param>
        /// <param name="dgvHead">DataGridViewHead</param>
        /// <param name="dv">绑定数据集</param>
        public static void setDgvStyle(ArrayList arr_style, ref DataGridViewHead dgvHead, DataView dv)
        {
            dgvHead.DataSource = dv;

            dgvHead.Columns.Clear();

            int i = 0;
            foreach (object col in arr_style)
            {
                Object[] obj = (Object[])col;

                if (obj != null && obj.Length == 15)
                {
                    if (obj[2].ToString() == "0" || obj[2].ToString() == "6")
                    {
                        dgvHead.Columns.Add(CreateTextBoxColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "1")
                    {
                        dgvHead.Columns.Add(CreateCheckBoxColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "2")
                    {
                        dgvHead.Columns.Add(CreateComboBoxColumn(obj[1].ToString(), obj[0].ToString(), (ArrayList)obj[14]));
                    }
                    else if (obj[2].ToString() == "3")
                    {
                        dgvHead.Columns.Add(CreateDateTimeColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else if (obj[2].ToString() == "7")
                    {
                        dgvHead.Columns.Add(CreateLinkColumn(obj[1].ToString(), obj[0].ToString()));
                    }
                    else
                    {
                        continue;
                    }

                    if (obj[3] != null && obj[3].ToString() != String.Empty)
                    {
                        dgvHead.Columns[i].Width = (int)obj[3];
                    }
                    if (obj[4] != null)
                    {
                        if (obj[4].ToString() == "c")
                        {
                            dgvHead.Columns[i].ValueType = typeof(Double);
                            if (obj[12] == null || obj[12].ToString() == String.Empty)
                            {
                                dgvHead.Columns[i].DefaultCellStyle.Format = "c";
                            }
                            else
                            {
                                dgvHead.Columns[i].DefaultCellStyle.Format = obj[12].ToString();
                            }
                        }
                        else if (obj[4].ToString() == "d")
                        {
                            dgvHead.Columns[i].ValueType = typeof(DateTime);
                            if (obj[12] == null || obj[12].ToString() == String.Empty)
                            {
                                dgvHead.Columns[i].DefaultCellStyle.Format = "d";
                            }
                            else
                            {
                                dgvHead.Columns[i].DefaultCellStyle.Format = obj[12].ToString();
                            }
                        }
                    }
                    dgvHead.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    if (obj[5] != null)
                    {
                        dgvHead.Columns[i].DefaultCellStyle.Alignment = (DataGridViewContentAlignment)obj[5];
                    }
                    else
                    {
                        dgvHead.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }
                    if (obj[6] != null)
                    {
                        dgvHead.Columns[i].ReadOnly = (bool)obj[6];
                    }
                    else
                    {
                        dgvHead.Columns[i].ReadOnly = true;
                    }
                    if (obj[7] != null)
                    {
                        dgvHead.Columns[i].DefaultCellStyle.NullValue = obj[7].ToString();
                    }


                    if (obj[2].ToString() == "6")
                    {
                        dgvHead.Columns[i].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    }

                    dgvHead.Columns[i].DisplayIndex = i;
                }

                i++;
            }

            dgvHead.AutoGenerateColumns = false;
        }


        /// <summary>
        /// DataGridView颜色设置 在DataGridView事件CellFormatting中调用
        /// </summary>
        /// <param name="dgv">DataGridView</param>
        /// <param name="e"></param>
        /// <param name="rowindex">列号</param>
        /// <param name="colorvalue">变色条件 每一个变色条件对应一个Object集合 0-列号，1-前景色，2-变色条件，当该列号值为该条件时进行变色，3-背景色</param>
        /// <param name="flag">0-整行变色；1-只该单元格变色</param>
        public static void setDgvColor(DataGridView dgv, DataGridViewCellFormattingEventArgs e, int rowindex, ArrayList colorvalue, string flag)
        {
            for (int j = 0; j < colorvalue.Count; j++)
            {
                Object[] obj = (Object[])colorvalue[j];
                if (dgv.Rows[e.RowIndex].Cells[rowindex].Value.ToString() == obj[2].ToString())
                {
                    if (flag == "0")
                    {
                        dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
                        dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.FromArgb((Int32)obj[1]);
                        dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb((Int32)obj[3]);
                    }
                    else if (flag == "1")
                    {
                        if (e.ColumnIndex == rowindex)
                        {
                            dgv.Rows[e.RowIndex].Cells[rowindex].Style.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
                            dgv.Rows[e.RowIndex].Cells[rowindex].Style.ForeColor = Color.FromArgb((Int32)obj[1]);
                            dgv.Rows[e.RowIndex].Cells[rowindex].Style.BackColor = Color.FromArgb((Int32)obj[3]);
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 显示DataGridView行号 在DataGridView事件RowPostPaint中调用该方法
        /// </summary>
        /// <param name="dgv">DataGridView</param>
        /// <param name="e"></param>
        public static void disDgvSerialno(DataGridView dgv, DataGridViewRowPostPaintEventArgs e) 
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                dgv.RowHeadersWidth - 4, e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font,
                rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        /// <summary>
        /// 调整DataGridView各列宽度使之适应grid的总宽度。
        /// 在刷新DataGridView和DataGridView_Resize时调用。
        /// </summary>
        /// <param name="dgv"></param>
        public static void SizeableDataGridViewColumn(DataGridView dgv)
        {
            if (dgv == null) return;

            int sumColWidth = 0;
            foreach (DataGridViewColumn dgvc in dgv.Columns)
            {
                if(dgvc.Visible) sumColWidth += dgvc.Width;
            }

            if (sumColWidth == 0) return;

            // 减4 因为边框宽度
            // 减10 因为最后留一点空，避免出现水平滚动条。
            int GridWidth = dgv.Width - dgv.RowHeadersWidth - 4 - 10;
            if (IsVScrollBarVisible(dgv)) GridWidth -= SystemInformation.VerticalScrollBarWidth;

            foreach (DataGridViewColumn dgvc in dgv.Columns)
            {
                if(dgvc.Visible) dgvc.Width = dgvc.Width * GridWidth / sumColWidth;
            }
        }

        /// <summary>
        ///	判断控件的垂直滚动条是否可见
        /// </summary>
        /// <param name="aControl"></param>
        /// <returns></returns>
        private static bool IsVScrollBarVisible(Control aControl)
        {
            foreach (Control c in aControl.Controls)
            {
                if (c.GetType().Equals(typeof(VScrollBar)))
                {
                    return c.Visible;
                }
            }
            return false;
        }


        #region 生成DataGridView列

        /// <summary>
        /// 生成CheckBox列
        /// </summary>
        /// <param name="colName"></param>
        /// <param name="chnName"></param>
        /// <returns></returns>
        private static DataGridViewCheckBoxColumn CreateCheckBoxColumn(string colName, string chnName)
        {
            DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1
                = new DataGridViewCheckBoxColumn();
            dataGridViewCheckBoxColumn1.HeaderText = chnName;
            dataGridViewCheckBoxColumn1.TrueValue = "是";
            dataGridViewCheckBoxColumn1.FalseValue = "否";
            dataGridViewCheckBoxColumn1.IndeterminateValue = "否";
            dataGridViewCheckBoxColumn1.ThreeState = false;
            dataGridViewCheckBoxColumn1.ValueType = typeof(String);
            dataGridViewCheckBoxColumn1.DataPropertyName = colName;
            dataGridViewCheckBoxColumn1.Name = colName;
            return dataGridViewCheckBoxColumn1;
        }

        private static DataGridViewTextBoxColumn CreateTextBoxColumn(string colName, string chnName)
        {
            DataGridViewTextBoxColumn tbc = new DataGridViewTextBoxColumn();
            tbc.HeaderText = chnName;
            tbc.ValueType = typeof(String);
            tbc.DataPropertyName = colName;
            tbc.Name = colName;
            return tbc;
        }

        private static DataGridViewComboBoxColumn CreateComboBoxColumn(string colName, string chnName, ArrayList arr_item)
        {
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.HeaderText = chnName;

            DataTable dt = new DataTable(colName);
            dt.Columns.Add("Display", typeof(String));
            dt.Columns.Add("Value", typeof(String));

            foreach (object obj in arr_item) 
            {
                Object[] item = (Object[])obj;
                dt.Rows.Add(item);
            }

            cbc.DataSource = dt;

            cbc.ValueMember = "Value";
            cbc.DisplayMember = "Display";

            cbc.DataPropertyName = colName;
            cbc.Name = colName;
            return cbc;
        }


        private static DataGridViewDateTimeColumn CreateDateTimeColumn(string colName, string chnName)
        {
            DataGridViewDateTimeColumn dtc = new DataGridViewDateTimeColumn();
            dtc.HeaderText = chnName;
            dtc.ValueType = typeof(DateTime);
            dtc.Name = colName;
            dtc.DataPropertyName = colName;
            return dtc;
        }

        private static DataGridViewLinkColumn CreateLinkColumn(string colName, string chnName)
        {
            DataGridViewLinkColumn dgvlc = new DataGridViewLinkColumn();
            dgvlc.HeaderText = chnName;
            dgvlc.Name = colName;
            dgvlc.ValueType = typeof(String);
            dgvlc.DataPropertyName = colName;
            return dgvlc;
        }

        private DataGridViewButtonColumn CreateButtonColumn(string colName, string chnName)
        {
            DataGridViewButtonColumn dgvbc = new DataGridViewButtonColumn();
            dgvbc.HeaderText = chnName;
            dgvbc.Text = chnName;
            dgvbc.Name = colName;
            dgvbc.UseColumnTextForButtonValue = true;
            dgvbc.DataPropertyName = colName;
            return dgvbc;
        }

        #endregion

    }
}
